package graphic.primitives.town;

import graphic.primitives.general.GraphicObject;
import graphic.primitives.general.IGraphicObject;
import graphic.window.MainWindow;
import java.awt.Color;
import java.util.Random;
import java.util.List;
import java.util.LinkedList;

/**
 *
 * @author Tomas Zima
 */
public class Building extends GraphicObject implements IGraphicObject
{
	public Building(int x, int y, int width, int height)
	{
		super(x, y);
		this.width	= width;
		this.height	= height;
		
		//float q		= (float) (new Random().nextInt() % 3) + 0.5f;
		float q			= (float) (new Random().nextInt() % 200) / 1000.0f + 0.5f;
		this.color	= new Color(q, q, q);
		
		for (int i = 10; i < width - 10; i += 15)
		{
			for (int j = 10; j < height - 10; j += 15)
			{
				windows.add(new Window(x + i, y - j));
			}
		}
	
	}
	
	@Override
	public void redraw()
	{
		mainWindow.setColor(color);
		
		for (int i = 0; i < height; i++)
		{
			mainWindow.line(x, y - i, x + width, y - i);
		}

		for (int i = 0; i < windows.size(); i++)
		{
			windows.get(i).redraw();
		}
		
		mainWindow.setColor(Color.BLACK);
	}
	
	private int				width;
	private	int				height;
	private Color			color;
	private List<Window>	windows = new LinkedList<Window>();
}

class Window
{
	public Window(int x, int y)
	{
		this.x = x;
		this.y = y;
		
		if (new Random().nextInt(5) == 0)
		{
			light = true;
		}
	}
	
	public void redraw()
	{
		MainWindow.getInstance().line(x, y, x + SIZE, y);
		MainWindow.getInstance().line(x, y - SIZE, x + SIZE, y - SIZE);
		MainWindow.getInstance().line(x, y, x, y - SIZE);
		MainWindow.getInstance().line(x + SIZE, y, x + SIZE, y - SIZE);
	
		if (new Random().nextInt(250) == 0)
		{
			light = !light;
		}
		
		if (light)
		{
			MainWindow.getInstance().setColor(Color.YELLOW);
		}
		else
		{
			MainWindow.getInstance().setColor(new Color(0.0f, 0.0f, 0.5f));
		}
		
		for (int i = 0; i < SIZE; i++)
		{
			MainWindow.getInstance().line(x, y - i, x + SIZE, y - i);
		}
		
		MainWindow.getInstance().setColor(Color.BLACK);
	}
	
	private int x;
	private int y;
	
	private final int SIZE = 5;
	
	boolean light = false;
}
